package com.sx.manage.controller.operation;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sx.common.util.DateUtil;
import com.sx.common.util.json.JackJsonUtil;
import com.sx.manage.controller.base.MBaseController;
import com.sx.manage.service.bank.IMConsumerBankMapService;
import com.sx.manage.service.company.IMBaseCompanyService;
import com.sx.manage.service.operation.IMBankAccountAdjustOrderInfoService;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.manage.util.WebServiceUtils;
import com.sx.model.constants.MAdjustStatus;
import com.sx.model.manage.mybatis.form.sys_operation.MBankAccountAdjustOrderInfoForm;
import com.sx.model.manage.mybatis.vo.company.MCompanyVO;
import com.sx.model.manage.mybatis.vo.operation.MBankAccountAdjustOrderInfoVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;

/**
 * 系统运维-对外账户调整
 * @author zwm
 *
 */
@Controller
@RequestMapping("/bank_account_adjust")
public class MBankAccountAdjustOrderInfoController extends MBaseController{
	
	private Logger _log = LoggerFactory.getLogger(this.getClass());
	
	@Resource(name="consumerBankMapServiceImpl")
	private IMConsumerBankMapService consumerBankMapServiceImpl;
	@Resource(name="mBankAccountAdjustOrderInfoServiceImpl")
	private IMBankAccountAdjustOrderInfoService mBankAccountAdjustOrderInfoServiceImpl;
	// 公司信息
	@Resource(name = "mBaseCompanyServiceImpl")
	private IMBaseCompanyService mBaseCompanyServiceImpl;
	
	@Resource(name="webServiceUtils")
	private WebServiceUtils webServiceUtils;
	/**
	 * 对外账户调整查询列表
	 * @return
	 */
	@RequestMapping(value="bank_account_adjust_view")
	public ViewAndModel query_viewBankAccountAdjust(){
		ViewAndModel model=new ViewAndModel(request,cache);
		model.setViewName("sysMaintain/bankTransfer/applyTransfer");//修改ftl页面名称
		return model;
	}
	/**
	 * 对外账户调整查询列表
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/bank_account_adjust_page")
	 @ResponseBody
	public ResultListVO<MBankAccountAdjustOrderInfoVO> search_bank_account_adjust(MBankAccountAdjustOrderInfoForm form){ 
		_log.debug("添加系统操作日志：MBankAccountAdjustOrderInfoController===search_bank_account_adjust==formJson="+JackJsonUtil.objToJson( form ));
		if ("".equals(form.getIn_account_name())) {
			form.setIn_account_name(null);
		}
		return mBankAccountAdjustOrderInfoServiceImpl.search_mBankAccountAdjustOrder(form);
	}
	
	/**
	 * 对外账户调整查询列表
	 * @return
	 */
	@RequestMapping(value="bank_account_add_view")
	public ViewAndModel add_viewBankAccountAdjust(){
		ViewAndModel model=new ViewAndModel(request,cache);
		model.setViewName("sysMaintain/bankTransfer/AddTransfer");//修改ftl页面名称
		
		MCompanyVO companyVO = mBaseCompanyServiceImpl.query_company_S_X();
		
		model.addObject("companyVO", companyVO);
		model.addObject("branchAccountNo",webServiceUtils.queryBranchAccountNoCompany(companyVO.getId()));
		return model;
	}
	
	@RequestMapping(value="bank_account_save")
	@ResponseBody
	public ResultBooleanVO add_bank_account_adjust(MBankAccountAdjustOrderInfoForm form){
		_log.debug("添加系统操作日志：MBankAccountAdjustOrderInfoController===add_bank_account_adjust==formJson="+JackJsonUtil.objToJson( form ));
		if(null == form){
			return new ResultBooleanVO("sj.m.common.add.error");
		}
		return mBankAccountAdjustOrderInfoServiceImpl.add_mBankAccountAdjustOrder(form,this.loginConsumer());
	}
	
	
	
	/**
	 * 技术审批
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/edit_artisan_status")
	@ResponseBody
	public ResultBooleanVO  edit_artisan_status(MBankAccountAdjustOrderInfoForm form){
		_log.debug("添加系统操作日志：MBankAccountAdjustOrderInfoController===edit_artisan_status==formJson="+JackJsonUtil.objToJson( form ));
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		if ("yes".equals(form.getType())) {
			form.setArtisan_status(MAdjustStatus.ADJUST_SUCCSS);
		}else if("no".equals(form.getType())){
			form.setStatus(MAdjustStatus.ADJUST_FAIL);
			form.setArtisan_status(MAdjustStatus.ADJUST_FAIL);
		}
		form.setArtisan_time(DateUtil.getTimestamp());
		return mBankAccountAdjustOrderInfoServiceImpl.edit_mBankAccountAdjustOrder(form,this.loginConsumer());
	}
	
	@RequestMapping("/query_bank_artisan_status")
	@ResponseBody
	public ResultBooleanVO query_bank_artisan_status(MBankAccountAdjustOrderInfoForm form){
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		//查询数据状态和技术状态是否是撤销数据和审核状态变化
		return mBankAccountAdjustOrderInfoServiceImpl.query_artisan_status(form);
	}
	
	/**
	 * 财务审批
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/edit_finance_status")
	@ResponseBody
	public ResultBooleanVO  edit_finance_status(MBankAccountAdjustOrderInfoForm form){
		_log.debug("添加系统操作日志：MBankAccountAdjustOrderInfoController===edit_finance_status==formJson="+JackJsonUtil.objToJson( form ));
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		if ("yes".equals(form.getType())) {
			form.setFinance_status(MAdjustStatus.ADJUST_SUCCSS);
		}else if("no".equals(form.getType())){
			form.setStatus(MAdjustStatus.ADJUST_FAIL);
			form.setFinance_status(MAdjustStatus.ADJUST_FAIL);
		}
		form.setFinance_time(DateUtil.getTimestamp());
		return mBankAccountAdjustOrderInfoServiceImpl.edit_mBankAccountAdjustOrder(form,this.loginConsumer());
	}
	
	@RequestMapping("/query_bank_finance_status")
	@ResponseBody
	public ResultBooleanVO query_finance_status(MBankAccountAdjustOrderInfoForm form){
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		//查询数据状态和技术状态是否是撤销数据和审核状态变化
		return mBankAccountAdjustOrderInfoServiceImpl.query_finance_status(form);
	}
	
	/**
	 * 产品审批
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/edit_product_status")
	@ResponseBody
	public ResultBooleanVO  edit_product_status(MBankAccountAdjustOrderInfoForm form){
		_log.debug("添加系统操作日志：MBankAccountAdjustOrderInfoController===edit_product_status==formJson="+JackJsonUtil.objToJson( form ));
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		if ("yes".equals(form.getType())) {
			form.setProduct_status(MAdjustStatus.ADJUST_SUCCSS);
		}else if("no".equals(form.getType())){
			form.setStatus(MAdjustStatus.ADJUST_FAIL);
			form.setProduct_status(MAdjustStatus.ADJUST_FAIL);
		}
		form.setProduct_time(DateUtil.getTimestamp());
		return mBankAccountAdjustOrderInfoServiceImpl.edit_mBankAccountAdjustOrder(form,this.loginConsumer());
	}
	
	@RequestMapping("/query_bank_product_status")
	@ResponseBody
	public ResultBooleanVO query_product_status(MBankAccountAdjustOrderInfoForm form){
		_log.debug("添加系统操作日志：MBankAccountAdjustOrderInfoController===query_product_status==formJson="+JackJsonUtil.objToJson( form ));
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		//查询数据状态和技术状态是否是撤销数据和审核状态变化
		return mBankAccountAdjustOrderInfoServiceImpl.query_product_status(form);
	}
	
	/**
	 * 领导审批
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/edit_boss_status")
	@ResponseBody
	public ResultBooleanVO  edit_boss_status(MBankAccountAdjustOrderInfoForm form){
		_log.debug("添加系统操作日志：MBankAccountAdjustOrderInfoController===edit_boss_status==formJson="+JackJsonUtil.objToJson( form ));
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		if ("yes".equals(form.getType())) {
			form.setBoss_status(MAdjustStatus.ADJUST_SUCCSS);
			form.setStatus(MAdjustStatus.ADJUST_FINISH);
		}else if("no".equals(form.getType())){
			form.setBoss_status(MAdjustStatus.ADJUST_FAIL);
			form.setStatus(MAdjustStatus.ADJUST_FAIL);
		}
		form.setBoss_time(DateUtil.getTimestamp());
		return mBankAccountAdjustOrderInfoServiceImpl.edit_mBankAccountAdjustOrder(form,this.loginConsumer());
	}
	
	@RequestMapping("/query_bank_boss_status")
	@ResponseBody
	public ResultBooleanVO query_bank_boss_status(MBankAccountAdjustOrderInfoForm form){
		_log.debug("添加系统操作日志：MBankAccountAdjustOrderInfoController===query_bank_boss_status==formJson="+JackJsonUtil.objToJson( form ));
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		//查询数据状态和技术状态是否是撤销数据和审核状态变化
		return mBankAccountAdjustOrderInfoServiceImpl.query_boss_status(form);
	}
	
	/**
	 * 撤销
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/edit_revocation_status")
	@ResponseBody
	public ResultBooleanVO  edit_revocation_status(MBankAccountAdjustOrderInfoForm form){
		_log.debug("添加系统操作日志：MBankAccountAdjustOrderInfoController===edit_revocation_status==formJson="+JackJsonUtil.objToJson( form ));
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		ResultBooleanVO resultVO =  mBankAccountAdjustOrderInfoServiceImpl.query_revocation_status(form);
		if (!resultVO.isSuccess()) {
			return resultVO;
		}
		form.setStatus(MAdjustStatus.ADJUST_FAIL);
		form.setArtisan_status(MAdjustStatus.ADJUST_FAIL);
		form.setFinance_status(MAdjustStatus.ADJUST_FAIL);
		form.setProduct_status(MAdjustStatus.ADJUST_FAIL);
		form.setBoss_status(MAdjustStatus.ADJUST_FAIL);
		return mBankAccountAdjustOrderInfoServiceImpl.edit_mBankAccountAdjustOrder(form,this.loginConsumer());
	}
	
	@RequestMapping("/query_bank_account_status")
	@ResponseBody
	public ResultBooleanVO  change_account_status(MBankAccountAdjustOrderInfoForm form){
		_log.debug("添加系统操作日志：MBankAccountAdjustOrderInfoController===change_account_status==formJson="+JackJsonUtil.objToJson( form ));
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		//控制调整账户余额
		ResultObjectVO<MBankAccountAdjustOrderInfoVO> resultObjectVO = mBankAccountAdjustOrderInfoServiceImpl.search_byId(form);
		MBankAccountAdjustOrderInfoVO vo = resultObjectVO.getObjVo();
		if (null == vo) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		if (!MAdjustStatus.ADJUST_FINISH.equals(vo.getStatus())) {
			return new ResultBooleanVO("sj.m.bank.account.data.status.error");
		}
		
		return new ResultBooleanVO(true,"sj.m.common.query_.success","查询成功");
	}
	
	/**
	 * 对外账户调整打印
	 * @return
	 */
	@RequestMapping(value="bank_account_print_view/{id}")
	public ViewAndModel bank_account_print_view(MBankAccountAdjustOrderInfoForm form){
		_log.debug("添加系统操作日志：MBankAccountAdjustOrderInfoController===bank_account_print_view==formJson="+JackJsonUtil.objToJson( form ));
		ViewAndModel model=new ViewAndModel(request,cache);
		ResultObjectVO<MBankAccountAdjustOrderInfoVO> resultObjectVO = mBankAccountAdjustOrderInfoServiceImpl.search_byId(form);
		 model.addObject("mBankAccountAdjustOrderVO", resultObjectVO.getObjVo());
		model.setViewName("sysMaintain/bankTransfer/PrintTransfer");//修改ftl页面名称
		return model;
	}

}
